<?php

namespace app\index\controller;
session_start();
use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class Seat extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($id)
    {
        if(empty($_SESSION['home'])){
           $this->error('请先登录', 'index/Login/index');
        }

        $sort = Db::table('l_sort')->where('id', $id)->find();
        $film = Db::table('l_film')->field('filmname')->where('id', $sort['film_id'])->find();
        $cinema = Db::table('l_cinema')->field('cinema')->where('id', $sort['cinema_id'])->find();
        $studio = Db::table('l_studio')->where('id', $sort['studio_id'])->find();
        @$sell = Db::table('l_sell')->where('sort_id', $id)->find()['sell'];

        return view('Seat/index', [
            'sort'=>$sort,
            'film'=>$film,
            'cinema'=>$cinema,
            'seat'=>$studio['studio_seat'],
            'sell'=>$sell
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {

        $rel = $request->post();
        if (!$rel['seat']){
            $this->error('您还没有选择座位!');
            exit;
        }

        $time = date('Y/m/d H:i');
        $sort = Db::table('l_sort')->where('id', $rel['sort_id'])->find();
        $studio = Db::table('l_studio')
                ->field('studio_name')
                ->where('id', $sort['studio_id'])
                ->find();

        //得出已售的座位
        $sell = '';
        $seat = explode('cart-item-', $rel['seat']);

        for ($i=1; $i < count($seat); $i++) {
            $sell .= "'".$seat[$i]."',";
        }

        //插入数据
        @$seat_1 = Db::table('l_sell')->where('sort_id', $rel['sort_id'])->find();
        if(empty($seat_1)){
            $data = [
              'sort_id'=>$rel['sort_id'],
              'sell'=>$sell
            ];
            Db::table('l_sell')->insert($data);
        }else {
            $list = [
                'sell' => $seat_1['sell'] . $sell
            ];
            Db::table('l_sell')->where('sort_id', $rel['sort_id'])->update($list);
        }

        //执行生成订单
        $rand = [];
        $pass = [];
        $random = '';
        $passom = '';

        $abc = 'NM1ZXCV6BYU2ERT4QWL5IOP37DFGHAS0JK98';
        //订单编号
        for($a=0; $a < count($seat)-1; $a++){
            for ($j=0; $j < 14; $j++) {
                $random .= mt_rand(0, 9);
            }
            $rand[$a] = $random;
            $random = '';
        }
        //取票密码
        for ($p=0; $p < count($seat)-1; $p++) {
            for ($i=0; $i < 10; $i++) {
                $passom .= $abc[mt_rand(0, 35)];
            }
            $pass[$p] = $passom;
            $passom = '';
        }

        for ($x=0; $x < count($seat)-1; $x++) {
            $order = ([
                'order'=>'EQ'.$rand[$x],
                'price'=>$rel['price'],
                'user_id'=>$_SESSION['home']['id'],
                'pass'=>$pass[$x],
                'sort_id'=>$rel['sort_id'],
                'film_name'=>$rel['filmname'],
                'cinema_name'=>$rel['cinema'],
                'studio_name'=>$studio['studio_name'],
                'time'=>$sort['time'],
                'seat'=>$seat[$x+1],
                'retime'=>$time
            ]);
            Db::table('l_order')->insert($order);
        }

        $this->success('购票完成', 'index/Center/index');

    }
}
